﻿Imports Mystery

Public Class HorizontalBackSigleReferenceView
    Implements IPropertyControl

    Private _property_info As System.Reflection.PropertyInfo
    Private _incoming_ref_property_info As ContentReference
    Private _content_property As ContentReference

    Private delete_bitmap As New BitmapImage(New Uri("/Images/DeleteRed.png", UriKind.RelativeOrAbsolute))

    Public Property property_info As System.Reflection.PropertyInfo Implements IPropertyControl.property_info
        Get
            Return _property_info
        End Get
        Set(value As System.Reflection.PropertyInfo)
            _content_property = value.getMysteryAttribute(Of ContentReference)()
            _label = _content_property.label
            _property_info = value
            _incoming_ref_property_info = _property_info.getMysteryAttribute(Of BackReferenceAttribute).incoming_ref.getMysteryAttribute(Of ContentReference)()
        End Set
    End Property

    Private _value As ICollection(Of IContent) = New List(Of IContent)

    Private Sub deleteSelection(selection As IContent)
        Dim current_value As New List(Of IContent)(_value)
        current_value.Remove(selection)
        Me.value = current_value
    End Sub

    Public Property value As Object Implements IPropertyControl.value
        Get
            Return _value
        End Get
        Set(value As Object)
            If TypeOf (value) Is BaseContentReference Then value = DirectCast(value, BaseContentReference).getAsEnum()
            Dim new_value As IEnumerable(Of IContent) = value
            If new_value.sameContents(_value) Then Exit Property
            _value = New List(Of IContent)(new_value)
            'interfance
            myPanel.Children.Clear()

            For Each new_content As IContent In _value
                If new_content Is Nothing Then Continue For
                Dim sp As New StackPanel With {.Orientation = Orientation.Horizontal}
                Dim cb As New Button
                Dim icb As IContentButton = new_content.GetType.getMysteryAttribute(Of ContentButtonAttribute)().createButton(new_content)
                AddHandler cb.Click, AddressOf icb.Press
                cb.Content = icb.getText
                cb.MinWidth = 100
                Dim image As Image = icb.getImage
                image.Height = 30
                image.Width = 30
                sp.Children.Add(image)
                sp.Children.Add(cb)

                Dim delete As New DeleteIcon
                Dim current_content As IContent = new_content

                delete.todo = Sub() deleteSelection(current_content)

                sp.Children.Add(delete)

                myPanel.Children.Add(sp)
            Next

            RaiseEvent valueChanged()

        End Set
    End Property

    Private _content As IContent

    Public Property content_item As Mystery.IContent Implements IPropertyControl.content
        Get
            Return _content
        End Get
        Set(value As Mystery.IContent)
            _content = value
            Me.value = _content_property.retrive()(_content)
        End Set
    End Property


    Private _label As String
    Public ReadOnly Property label As String Implements IPropertyControl.label
        Get
            Return _label
        End Get
    End Property


    Public ReadOnly Property wished_height As System.Windows.GridLength Implements IPropertyControl.wished_height
        Get
            Return GridLength.Auto
        End Get
    End Property

    Public Function setInContent() As IEnumerable(Of IContent) Implements IPropertyControl.setInContent
        Dim current_value As BaseContentReference = _content_property.retrive()(_content)
        Dim guids As New HashSet(Of Guid)(current_value.getAsGuidEnum())
        For Each v As IContent In _value
            Dim pv As BaseContentReference = _incoming_ref_property_info.retrive()(v)
            pv.setValue(_content)
            guids.Remove(v.guid)
        Next
        Dim result As New List(Of IContent)(_value.Count + guids.Count)
        result.addRange(_value)
        'removing from those which shall not have it anylonger
        Dim cd As IContentDispatcher = getGlobalObject(Of IContentDispatcher)()
        For Each Guid As Guid In guids
            Dim v As IContent = cd.getContent(Of IContent)(Guid)
            Dim pv As BaseContentReference = _incoming_ref_property_info.retrive()(v)
            pv.setValue(Nothing)
            result.Add(v)
        Next
        Return result
    End Function

    Private Sub ContentSelected(content As IContent)
        Dim current As New List(Of IContent)
        current.addRange(value)
        current.Add(content)
        Me.value = current
        RaiseEvent valueChanged()
    End Sub

    Private Sub bAdd_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles bAdd.Click
        Dim controler As IContentSelectionControler = _property_info.getMysteryAttribute(Of ContentSelectionControlerAttr).createControler(_content)
        Dim w As New ContentSelectionWindow
        w.Controler = controler
        AddHandler w.contentSelected, AddressOf ContentSelected
        Dim screen_coo As Point = bAdd.PointToScreen(New Point)
        w.Left = screen_coo.X
        w.Top = screen_coo.Y
        w.ShowDialog()
        RemoveHandler w.contentSelected, AddressOf ContentSelected
    End Sub

    Public Event valueChanged() Implements IPropertyControl.valueChanged
End Class
